<?php
/**
 * Cycle helper for use within a partial loop
 * The cycle view helper doesn't work within a partialLoop because partials, by
 * definition, only have access to their own "context" variables.
 *
 * @package     Visi
 * @subpackage  View Helpers
 * @author      Luke Visinoni <luke.visinoni@gmail.com>
 * @copyright   (c) 2011 Coalition Technologies <joel@coalitiontechnologies.com>
 * @version     $Id$
 */
class Visi_View_Helper_PartialLoopCycle extends Zend_View_Helper_Abstract implements Iterator {

    /**
     * Default name
     * @var string
     */
    const DEFAULT_NAME = 'default';
    /**
     * This is the array that is cycled through
     */
    static protected $_data = array(self::DEFAULT_NAME => array());
    
    /**
     * Internal pointer, keeps track of the item we are on
     */
    static protected $_pointers = array(self::DEFAULT_NAME => -1);
    
    /**
     * Actual name of cycle
     */
    protected $_name = self::DEFAULT_NAME;
    
    /**
     * This is the method that is called when the helper is called in a template
     */
    public function partialLoopCycle(array $data = array(), $name = self::DEFAULT_NAME) {
    
        $this->setName($name);
        $this->assign($data, $name);
        return $this;
    
    }
    
    public function setName($name) {
    
        $this->_name = $name;
        if (!isset(self::$_data[$this->_name])) {
            self::$_data[$this->_name] = array();
        }
        if (!isset(self::$_pointers[$this->_name])) {
            $this->rewind();
        }
        return $this;
    
    }
    
    public function getName() {
    
        return $this->_name;
    
    }
    
    public function getAll() {
    
        return self::$_data[$this->_name];
    
    }
    
    /**
     * Resets the data that is used to cycle through
     */
    public function assign(array $data, $name = null) {
    
        if (is_null($name)) {
            $name = $this->getName();
        }
        self::$_data[$name] = $data;
    
    }
    
    public function toString() {
    
        return (string) self::$_data[$this->_name][$this->key()];
    
    }
    
    public function __toString() {
    
        return $this->toString();
    
    }
    
    public function next() {
    
        $count = count(self::$_data[$this->_name]);
        if (self::$_pointers[$this->_name] == ($count - 1)) {
            self::$_pointers[$this->_name] = 0;
        } else {
            self::$_pointers[$this->_name] = ++self::$_pointers[$this->_name];
        }
        return $this;
    
    }
    
    public function prev() {
    
        $count = count(self::$_data[$this->_name]);
        if (self::$_pointers[$this->_name] <= 0) {
            self::$_pointers[$this->_name] = $count - 1;
        } else {
            self::$_pointers[$this->_name] = --self::$_pointers[$this->_name];
        }
        return $this;
    
    }
    
    public function key() {
    
        if (self::$_pointers[$this->_name] < 0) {
            return 0;
        } else {
            return self::$_pointers[$this->_name];
        }
    
    }
    
    public function rewind() {
    
        self::$_pointers[$this->_name] = -1;
        return $this;
    
    }
    
    public function valid() {
    
        return isset(self::$_data[$this->_name][$this->key()]);
    
    }
    
    public function current() {
    
        return self::$_data[$this->_name][$this->key()];
    
    }

}